'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview boss
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var moment = require('moment');
var async = require('async');
var constantUtil = require('../util/constantUtil');
var langUtil = require('../util/langUtil');
var bossConf = require('../config/bossConf');
var genRewardService = require('./genRewardService');
var mailService = require('../services/mailService.js');
var bossModel = require('../models/bossModel');
var globalBossModel = require('../models/globalBossModel');
var roleModel = require('../models/roleModel');
var dataApi = require('../libs/dataApi.js');
var playerManager = require('../libs/playerManager.js');
var moduleType = require('../consts/moduleType');
var operationType = require('../consts/operationType');

/**
 * 是否能进入boss
 * @return {Boolean} [description]
 */
exports.isCanEnter = function () {
    var current_time = moment().unix();
    var boss_start_time = moment().startOf('day').unix() + constantUtil.value('boss_start_time');
    var boss_end_time = boss_start_time + constantUtil.value('boss_end_time');
    return current_time >= boss_start_time && current_time <= boss_end_time;
};

/**
 * 下次boss开放的时间
 * @return {[type]} [description]
 */
exports.nextOpenTime = function () {
    var current_time = moment();
    var boss_start_time = moment().startOf('day').add(
        constantUtil.value('boss_start_time'),
        's'
    );
    var boss_end_time = moment().startOf('day').add(
        constantUtil.value('boss_end_time'),
        's'
    );
    if (current_time <= boss_end_time) {
        return boss_start_time;
    } else {
        return moment().endOf('day').add(constantUtil.value('boss_start_time'), 's');
    }
};

/**
 * 发放排行奖励
 * @return {[type]} [description]
 */
exports.doBossRankReward = function (cb) {
    var count = dataApi.boss_rank_reward.length();
    bossModel.getTopRank(count, function (err, top_role) {
        var rank_num = 0;
        var role_list = [];
        _.forEach(top_role, function (role_item) {
            rank_num++;
            role_list.push({
                role_id: role_item.role_id,
                rank: rank_num
            });
        });

        globalBossModel.getGlobalBossInfo(function (err, global_boss_model) {
            var boss_id = global_boss_model.boss_id;
            var boss_name = dataApi.boss_type.findById(boss_id).name;

            // 取出对应职业
            var player_manager;
            async.forEachSeries(role_list, function (role_item, inner_cb) {
                player_manager = new playerManager(role_item.role_id);
                player_manager.getModel(['role', 'mail', 'accrued'], function () {
                    var role_model = player_manager.role;
                    var mail_model = player_manager.mail;
                    var job_type = role_model.getJob();
                    var reward_list = bossConf.getBossRewardConf(job_type, role_item.rank);

                    // 发放单笔充值礼包
                    var title = langUtil.getString('boss_rank_reward_mail_title');
                    var content = langUtil.getString('boss_rank_reward_mail_content');
                    title = title.replace(/{BOSS_NAME}/g, boss_name);
                    content = content.replace(/{BOSS_NAME}/g, boss_name);
                    mailService.sendMail(mail_model, title, content, reward_list, moduleType.MDL_WORLD_BOSS, operationType.OPR_WORLD_BOSS_RANK_REWARD);

                    mail_model.save(inner_cb());
                });
            }, function (err) {
                if (!!err) {
                    console.error(err);
                }
                if (!!cb) {
                    cb();
                }
            });
        });
    });
};
